import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_demo/res/colors.dart';
import 'package:flutter_demo/res/gaps.dart';
import 'package:flutter_demo/res/resources.dart';
import 'package:flutter_demo/util/theme_utils.dart';


//自定义AppBar
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  final Color backgroundColor;
  final String title;
  final String centerTitle;
  final String backImg;
  final String actionName;
  final VoidCallback onPressed;
  final bool isBack;

  const MyAppBar({
    Key key,
    this.backgroundColor,
    this.title:'',
    this.centerTitle:'',
    this.actionName:'',
    this.backImg:'assets/images/ic_back_black.png',
    this.onPressed,
    this.isBack:true
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Color _backgroundColor;
    if (_backgroundColor == null){
      _backgroundColor = ThemeUtils.getBackgroundColor(context);
    }else{
      _backgroundColor = backgroundColor;
    }

    //黑色主题切换
    SystemUiOverlayStyle _overlayStyle = ThemeData.estimateBrightnessForColor(_backgroundColor) == Brightness.dark
        ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;

    //返回按钮
    var back = isBack ? IconButton(
      onPressed: (){
        FocusScope.of(context).unfocus();
        Navigator.maybePop(context);
      },
      tooltip: 'Back',
      padding: const EdgeInsets.all(12.0),
      icon:Image.asset(
        backImg,
        color:ThemeUtils.getIconColor(context),
      ),
    ) : Gaps.empty;

    //动作按钮
    var action = actionName.isNotEmpty ? Positioned(
      right: 0.0,
      child: Theme(
        data: Theme.of(context).copyWith(
          buttonTheme:ButtonThemeData(
            padding: const EdgeInsets.symmetric(horizontal:16.0),
            minWidth: 60.0,
          )
        ),
        child: FlatButton(
          child: Text(actionName,key:const Key('actionName')),
          textColor: ThemeUtils.isDark(context)? Colours.dark_text:Colours.text,
          highlightColor: Colors.transparent,
          onPressed: onPressed,
        )
      ),
    ) : Gaps.empty;

    //自定义标题
    var titleWidget = Semantics(
      namesRoute: true,
      header:true,
      child: Container(
        alignment: centerTitle.isEmpty? Alignment.centerLeft:Alignment.center,
        width: double.infinity,
        child: Text(
          title.isEmpty?centerTitle:title,
          style:TextStyle(fontSize:Dimens.font_sp18,)
        ),
      )
    );

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: _overlayStyle,
      child: Material(
        color:_backgroundColor,
        child:SafeArea(
          child:Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              titleWidget,
              back,
              action,
            ],
          )
        )
      ),
    );
    
  }

  @override
  // TODO: implement preferredSize
  Size get preferredSize => Size.fromHeight(48.0);
}